<template>
  <div class="points-rank-container">
    <h1>积分排行</h1>
    <el-tabs v-model="activeTab" @tab-click="fetchRankData">
      <el-tab-pane label="总积分排行榜" name="total"></el-tab-pane>
      <el-tab-pane label="今日积分排行" name="today"></el-tab-pane>
      <el-tab-pane label="本周积分排行" name="week"></el-tab-pane>
      <el-tab-pane label="本月积分排行" name="month"></el-tab-pane>
    </el-tabs>
    <div class="rank-list">
      <el-card v-if="selfRank" class="self-rank-card">
        <div class="self-rank">
          <div class="rank-info">
            <i class="el-icon-star-on rank-icon"></i>
            <div class="rank-label">我的积分排名：</div>
            <div class="rank-value">{{ selfRank.rank }}</div>
          </div>
          <div class="rank-info">
            <i class="el-icon-trophy rank-icon"></i>
            <div class="rank-label">总积分：</div>
            <div class="rank-value">{{ selfRank.points }}</div>
          </div>
        </div>
      </el-card>
      <div class="separator" v-if="selfRank"></div>
      <el-table :data="paginatedRankList" style="width: 100%" :header-cell-style="{ background: 'transparent', color: '#000' }">
        <el-table-column prop="rank" label="排行" width="180"></el-table-column>
        <el-table-column prop="points" label="积分" width="180"></el-table-column>
        <el-table-column label="用户">
          <template slot-scope="scope">
            {{ scope.row.userId }} ({{ scope.row.userName }})
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="rankList.length"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handlePageChange"
      style="margin-top: 20px; text-align: center;"
    ></el-pagination>
  </div>
</template>

<script>
import requestUtil from '../../network/request'
import Cookie from "js-cookie";

export default {
  name: 'PointsRank',
  data() {
    return {
      activeTab: 'total',
      userId: Cookie.get('userId'),
      rankList: [],
      selfRank: null,
      currentPage: 1,
      pageSize: 10,
    };
  },
  mounted() {
    this.fetchRankData();
  },
  computed: {
    paginatedRankList() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.rankList.slice(start, end);
    },
  },
  methods: {
    fetchRankData() {
      const params = {
        userId: this.userId,
        currentPage: 1,
        size: 10,
      };

      requestUtil.post('/user_points/rank', params).then(res => {
        if (res.code === '0') {
          this.rankList = res.result.rankList;
          this.selfRank = this.rankList.find(item => item.userId === this.userId);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
  },
};
</script>

<style scoped>
.points-rank-container {
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
}

.rank-list {
  margin-top: 20px;
}

.self-rank-card {
  margin-bottom: 20px;
}

.self-rank {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px;
}

.rank-info {
  display: flex;
  align-items: center;
}

.rank-icon {
  font-size: 24px;
  margin-right: 10px;
  color: #67c23a;
}

.rank-label {
  font-size: 16px;
  font-weight: bold;
  color: #000;
}

.rank-value {
  font-size: 18px;
  font-weight: bold;
  color: #67c23a;
}

.separator {
  border-top: 1px solid #ddd;
  margin: 10px 0;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.el-table th {
  background-color: transparent;
  color: #000;
}

.el-table tr:hover {
  background-color: #f0f9eb;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
